from PySide6.QtCore import QSortFilterProxyModel


class DirectSortProxyModel(QSortFilterProxyModel):
    def sort(self, column, order):
        """Delegate sorting directly to source model"""
        if self.sourceModel():
            self.sourceModel().sort(column, order)
        self.invalidate()  # 通知视图更新

    def export_data(self, file_path):
        source = self.sourceModel()
        try:
            source.export_data(file_path)
        except AttributeError:
            raise NotImplementedError(
                f"Source model {type(source).__name__} does not implement export_data()"
            )

    def import_data(self, file_path):
        source = self.sourceModel()
        try:
            source.import_data(file_path)
        except AttributeError:
            raise NotImplementedError(
                f"Source model {type(source).__name__} does not implement import_data()"
            )

    def add_one_row(self, student, quiet=False):
        source = self.sourceModel()
        try:
            source.add_student(student, quiet=quiet)
        except AttributeError:
            raise NotImplementedError(
                f"Source model {type(source).__name__} does not implement add_student()"
            )

    def remove_rows(self, row_indices):
        source = self.sourceModel()
        try:
            # 将筛选后的行号转换为源模型的行号
            source_row_indices = [
                self.mapToSource(self.index(row, 0)).row()
                for row in row_indices
                if self.mapToSource(self.index(row, 0)).isValid()
            ]
            source.delete_students(source_row_indices)
        except AttributeError:
            raise NotImplementedError(
                f"Source model {type(source).__name__} does not implement delete_students()"
            )